package com.atguigu.minio;

import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class MinioDemo {
    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        //1.创建minioclient对象
        MinioClient minioClient = MinioClient.builder()
                .endpoint("http://192.168.174.100:9000")
                .credentials("minioadmin","minioadmin")
                .build();

        //2. 如果桶不存在，则创建桶
        boolean flag = minioClient.bucketExists(BucketExistsArgs.builder().bucket("b002").build());
        if(!flag){
            //2-1. 创建桶
            minioClient.makeBucket(MakeBucketArgs.builder().bucket("b002").build());
            //2-2. 设置桶访问策略
            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket("b002").config("""
                    {
                      "Statement" : [ {
                        "Action" : "s3:GetObject",
                        "Effect" : "Allow",
                        "Principal" : "*",
                        "Resource" : "arn:aws:s3:::b002/*"
                      } ],
                      "Version" : "2012-10-17"
                    }
                    """).build());
        }

        //3.上传对象
        minioClient
                .uploadObject(UploadObjectArgs.builder()
                        .bucket("b002")
                        .filename("D:\\atguigu2024\\workspace202409\\pro-lease\\pro03-minio\\src\\main\\resources\\imgs\\Linux.png")
                        .object("虚拟机操作系统.png")
                .build());

    }
}
